import {
  RebateModel
} from '../../../../models/rebate'
import {
  formatDate
} from '../../../../utils/index'
const App = getApp();

const rebateModel = new RebateModel()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    winHeight: "",
    userEarningsList: null,
    loading: false,
    hasNext: true,
    empty: false,
    statusBarHeight: App.globalData.statusBarHeight,
    isTabbar: false
  },

  // 开始时间
  bindStartDateChange: function (e) {
    this.setData({
      startDate: e.detail.value
    })
    this.setData({
      pageIndex: 1,
      pageSize: 10
    })
    this.initData()
  },

  // 结束时间
  bindEndDateChange: function (e) {
    this.setData({
      endDate: e.detail.value
    })
    this.setData({
      pageIndex: 1,
      pageSize: 10
    })
    this.initData()
  },
  getRebateDefault: function () {
    // this.data.pageIndex = 1
    const params = {
      // startDate: this.data.startDate,
      // endDate: this.data.endDate,
      // pageIndex: this.data.pageIndex,
      // pageSize: this.data.pageSize,
    }
    const self = this;
    rebateModel.queryAchievements(params).then(res => {
      if (res.data.userEarningsList.length == 0) {
        self.data.empty = true
      } else {
        self.data.empty = false
      }
      self.setData({
        empty: self.data.empty,
        userEarningsList: res.data.userEarningsList,
        hasNext: res.data.hasNext,
        loading: false
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  initData: function () {
    this.getRebateDefault()
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // if (!this.data.startDate) {
    //   this.setData({
    //     startDate: formatDate((new Date()).getTime(), 'yyyy-MM-dd')
    //   })
    // }
    // if (!this.data.endDate) {
    //   this.setData({
    //     endDate: formatDate((new Date()).getTime(), 'yyyy-MM-dd')
    //   })
    // }
    // this.setData({
    //   today: formatDate((new Date()).getTime(), 'yyyy-MM-dd')
    // })
    this.initData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // this.setData({
    //   startDate: formatDate((new Date()).getTime(), 'yyyy-MM-dd'),
    //   endDate: formatDate((new Date()).getTime(), 'yyyy-MM-dd'),
    //   pageIndex: 1,
    //   pageSize: 10
    // })
    this.initData()
    wx.stopPullDownRefresh()
  },

  //加载更多
  loadMore(isPull) {
    if (this.data.loading) {
      return
    }
    if (this.data.hasNext) {
      const params = {
        // startDate: this.data.startDate,
        // endDate: this.data.endDate,
        // pageIndex: this.data.pageIndex,
        // pageSize: this.data.pageSize,
      }
      if (isPull) {
        this.setData({
          loading: true
        })
      }
      rebateModel.queryAchievements(params).then(res => {
        const tempArray = this.data.userEarningsList.concat(res.data.userEarningsList)
        this.setData({
          userEarningsList: tempArray,
          hasNext: res.data.hasNext,
          loading: false
        })
      })

    }
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.hasNext) {
      return
    }
    // this.data.pageIndex = this.data.pageIndex + 1
    this.loadMore(true)
  },

})